package com.dkm.ec.activity.domain;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.biz.gl.exception.PartinException;
import com.dkm.ec.activity.persistence.dao.CommonActivityConfigQueryDao;
import com.dkm.ec.activity.persistence.dao.CommonActivityPartinQueryDao;
import com.dkm.ec.activity.persistence.dao.CommonActivitySkuDao;
import com.dkm.ec.activity.persistence.dao.CommonActivitySkuQueryDao;
import com.dkm.ec.activity.persistence.entity.CommonActivityConfigPo;
import com.dkm.ec.activity.persistence.entity.CommonActivityPartinPo;
import com.dkm.ec.activity.persistence.entity.CommonActivitySkuPo;
import com.dkm.ec.so.persistence.entity.SoItemPo;
import com.dkm.ec.stock.persistence.dao.StockDao;
import com.dkm.ec.stock.persistence.dao.StockQueryDao;
import com.dkm.ec.stock.persistence.entity.StockPo;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author hhq
 * @create 2017/8/14
 */
@SuppressWarnings("serial")
@Service
@Scope("prototype")
public class CommonActivitySku extends AbstractDomain<String, CommonActivitySkuPo> {

    private CommonActivitySkuDao commonActivitySkuDao = null;

    @Resource
    private CommonActivitySkuQueryDao commonActivitySkuQueryDao;
    @Resource
    private CommonActivityPartinQueryDao commonActivityPartinQueryDao;
    @Resource
    private CommonActivityConfigQueryDao commonActivityConfigQueryDao;
    @Resource
    private StockDao stockDao;
    @Resource
    private StockQueryDao stockQueryDao;

    @Override
    protected void init() {
        commonActivitySkuDao = SpringHelper.getBean(CommonActivitySkuDao.class);
        setDao(commonActivitySkuDao);
    }

    /**
     * 扣除商品
     * @param soItemPo
     */
    public void deduct(SoItemPo soItemPo) {
        if (soItemPo.getActivityType().equals("3") || soItemPo.getActivityType().equals("4") || soItemPo.getActivityType().equals("5")) {
            CommonActivitySkuPo skuPo = commonActivitySkuQueryDao.get(soItemPo.getActSkuId() + "");
            CommonActivityPartinPo partinPo = commonActivityPartinQueryDao.get(skuPo.getPartinid());
            CommonActivityConfigPo configPo = commonActivityConfigQueryDao.get(partinPo.getCommonactivityid());
            Date currentTime = new Date();
            if(currentTime.after(configPo.getBeginTime()) && currentTime.before(configPo.getEndTime())) {
                if(soItemPo.getCount() > skuPo.getAvailableNum()) {
                    throw new PartinException(String.format("id：%s 商品不足", soItemPo.getId()));
                }
                skuPo.setAvailableNum(skuPo.getAvailableNum() - soItemPo.getCount());
                commonActivitySkuDao.update(skuPo);
            }else {
                throw new PartinException(String.format("id：%s 不在活动时间范围内", soItemPo.getId()));
            }
        }else {
            throw new PartinException(String.format("id：%s 活动类型有误", soItemPo.getId()));
        }
    }

    /**
     * 回退商品
     * @param soItemPo
     */
    public void sendBack(SoItemPo soItemPo) {
        if(soItemPo.getActivityType().equals("3") || soItemPo.getActivityType().equals("4") || soItemPo.getActivityType().equals("5")) {
            CommonActivitySkuPo skuPo = commonActivitySkuQueryDao.get(soItemPo.getActSkuId() + "");
            // 判断是否归还总库存
            if("0".equals(skuPo.getIsReturnStock())) { // 0: 没有归还总库存
                skuPo.setAvailableNum(skuPo.getAvailableNum() + soItemPo.getCount());
                commonActivitySkuDao.update(skuPo);
            }else { // 1: 已经归还总库存
                StockPo stockPo = stockQueryDao.get(skuPo.getStockid());
                stockPo.setQty(stockPo.getQty() + soItemPo.getCount());
                stockDao.update(stockPo);
            }
        }else {
            throw new PartinException(String.format("id：%s 活动类型有误", soItemPo.getId()));
        }
    }
}
